package com.getpebble.android.bluetooth.receiver;

import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import com.getpebble.android.common.core.trace.Trace;
import java.lang.ref.WeakReference;

public abstract class BondReceiver extends BroadcastReceiver {
    private static final String TAG = BondReceiver.class.getSimpleName();
    private WeakReference<Context> mContextWeakReference = null;

    public abstract void onDeviceBonded(String str);

    public abstract void onDevicePairingRequest(String str);

    public abstract void onDeviceUnbonded(String str);

    public BondReceiver(Context context) throws IllegalArgumentException {
        if (context == null) {
            throw new IllegalArgumentException("'context' cannot be null!");
        }
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("android.bluetooth.device.action.BOND_STATE_CHANGED");
        intentFilter.addAction("android.bluetooth.device.action.PAIRING_REQUEST");
        context.registerReceiver(this, intentFilter);
        this.mContextWeakReference = new WeakReference(context);
    }

    public void onReceive(Context context, Intent intent) {
        if (intent == null) {
            Trace.warning(TAG, "onReceive: intent is null");
            return;
        }
        Trace.verbose(TAG, "onReceive: " + intent.getAction());
        String action = intent.getAction();
        if ("android.bluetooth.device.action.BOND_STATE_CHANGED".equals(action)) {
            handleBondingEvents(intent);
        } else if ("android.bluetooth.device.action.PAIRING_REQUEST".equals(action)) {
            handlePairingRequest(intent);
        }
    }

    private void handleBondingEvents(Intent intent) {
        int prevBondState = intent.getIntExtra("android.bluetooth.device.extra.PREVIOUS_BOND_STATE", -1);
        int bondState = intent.getIntExtra("android.bluetooth.device.extra.BOND_STATE", -1);
        BluetoothDevice device = (BluetoothDevice) intent.getParcelableExtra("android.bluetooth.device.extra.DEVICE");
        Trace.verbose(TAG, "device = " + device.getAddress() + " prev state = " + prevBondState + " new state = " + bondState);
        if (prevBondState == 12 && bondState == 10) {
            onDeviceUnbonded(device.getAddress());
        } else if (prevBondState == 11 && bondState == 12) {
            onDeviceBonded(device.getAddress());
        }
    }

    private void handlePairingRequest(Intent intent) {
        BluetoothDevice device = (BluetoothDevice) intent.getParcelableExtra("android.bluetooth.device.extra.DEVICE");
        if (device == null) {
            Trace.warning(TAG, "handlePairingRequest: device is null");
        } else {
            onDevicePairingRequest(device.getAddress());
        }
    }

    public void stopReceiving() {
        Context context = (Context) this.mContextWeakReference.get();
        if (context != null) {
            context.unregisterReceiver(this);
        }
    }
}
